import os
from typing import Optional
from dotenv import load_dotenv
from pydantic_settings import BaseSettings

# 加载环境变量
load_dotenv()

class Settings(BaseSettings):
    """项目配置设置"""
    
    # 智谱清言 API配置
    zhipu_api_key: str = os.getenv("ZHIPU_API_KEY", "")
    zhipu_base_url: Optional[str] = os.getenv("ZHIPU_BASE_URL", "https://open.bigmodel.cn/api/paas/v4/")
    
    # 项目配置
    project_name: str = os.getenv("PROJECT_NAME", "CodeBuddy")
    debug: bool = os.getenv("DEBUG", "false").lower() == "true"
    log_level: str = os.getenv("LOG_LEVEL", "INFO")
    
    # Web界面配置
    streamlit_port: int = int(os.getenv("STREAMLIT_SERVER_PORT", "8501"))
    streamlit_server_port: Optional[int] = int(os.getenv("STREAMLIT_SERVER_PORT", "8501"))
    fastapi_port: int = int(os.getenv("FASTAPI_PORT", "8000"))
    
    # AI模型配置
    default_model: str = "glm-4-flash"
    temperature: float = 0.7
    max_tokens: int = 2000
    
    model_config = {
        "env_file": ".env",
        "extra": "ignore"  # 忽略额外的字段，避免验证错误
    }

# 全局设置实例
settings = Settings()

# 验证配置
def validate_config():
    """验证配置是否完整"""
    if not settings.zhipu_api_key:
        raise ValueError("❌ ZHIPU_API_KEY 未设置！请在 .env 文件中配置智谱清言API密钥。")
    
    print(f"✅ 配置验证通过!")
    print(f"📊 项目名称: {settings.project_name}")
    print(f"🤖 默认模型: {settings.default_model}")
    print(f"🔧 调试模式: {settings.debug}")
    print(f"🔑 API Key: {settings.zhipu_api_key[:8]}...")
    
if __name__ == "__main__":
    validate_config() 